{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a8e5a67a-c055-48f1-859f-d73ce74db86c",
   "metadata": {},
   "source": [
    "# Base基础篇"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea17c61c-21c0-465a-a827-887a98906d48",
   "metadata": {},
   "source": [
    "### 1. 创建张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "de91814f-8206-4a39-974c-8432bb0f85ea",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:41:30.602311Z",
     "iopub.status.busy": "2024-02-14T07:41:30.601303Z",
     "iopub.status.idle": "2024-02-14T07:41:30.612815Z",
     "shell.execute_reply": "2024-02-14T07:41:30.610213Z",
     "shell.execute_reply.started": "2024-02-14T07:41:30.602311Z"
    }
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "58064226-7fc0-4b12-98b5-d2e6937ca32f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:42:44.403383Z",
     "iopub.status.busy": "2024-02-14T07:42:44.401381Z",
     "iopub.status.idle": "2024-02-14T07:42:46.092543Z",
     "shell.execute_reply": "2024-02-14T07:42:46.090565Z",
     "shell.execute_reply.started": "2024-02-14T07:42:44.403383Z"
    }
   },
   "outputs": [],
   "source": [
    "a = tf.constant([1,5],dtype = tf.int64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b2b6356a-7e83-4cdb-8e14-6a5649811a71",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:42:46.139589Z",
     "iopub.status.busy": "2024-02-14T07:42:46.138971Z",
     "iopub.status.idle": "2024-02-14T07:42:46.155011Z",
     "shell.execute_reply": "2024-02-14T07:42:46.153003Z",
     "shell.execute_reply.started": "2024-02-14T07:42:46.139589Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(2,), dtype=int64, numpy=array([1, 5], dtype=int64)>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e1945f12-c872-4bee-ba40-5c76cb3d479d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:43:01.487445Z",
     "iopub.status.busy": "2024-02-14T07:43:01.485934Z",
     "iopub.status.idle": "2024-02-14T07:43:01.498997Z",
     "shell.execute_reply": "2024-02-14T07:43:01.497473Z",
     "shell.execute_reply.started": "2024-02-14T07:43:01.487445Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tf.int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "661bda70-8a88-4291-94f6-293c443a4613",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:43:11.848356Z",
     "iopub.status.busy": "2024-02-14T07:43:11.848356Z",
     "iopub.status.idle": "2024-02-14T07:43:11.865331Z",
     "shell.execute_reply": "2024-02-14T07:43:11.863429Z",
     "shell.execute_reply.started": "2024-02-14T07:43:11.848356Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TensorShape([2])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85915d29-0a5d-465c-81e7-76cb7168e643",
   "metadata": {},
   "source": [
    "#### numpy convert to tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "637e05ae-3f61-420e-a1d0-d59951be7081",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:46:33.407233Z",
     "iopub.status.busy": "2024-02-14T07:46:33.406256Z",
     "iopub.status.idle": "2024-02-14T07:46:33.419899Z",
     "shell.execute_reply": "2024-02-14T07:46:33.416420Z",
     "shell.execute_reply.started": "2024-02-14T07:46:33.407233Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "89515386-8062-46e2-b6c5-3efc2b6d0e52",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:46:33.995653Z",
     "iopub.status.busy": "2024-02-14T07:46:33.994654Z",
     "iopub.status.idle": "2024-02-14T07:46:34.008348Z",
     "shell.execute_reply": "2024-02-14T07:46:34.006831Z",
     "shell.execute_reply.started": "2024-02-14T07:46:33.995653Z"
    }
   },
   "outputs": [],
   "source": [
    "a1 = np.arange(0,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5c304577-30ce-499c-9e7e-f8f8219c4840",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:46:34.543853Z",
     "iopub.status.busy": "2024-02-14T07:46:34.542810Z",
     "iopub.status.idle": "2024-02-14T07:46:34.566713Z",
     "shell.execute_reply": "2024-02-14T07:46:34.565778Z",
     "shell.execute_reply.started": "2024-02-14T07:46:34.543853Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2a42cd90-71a4-4580-b7d7-47994034e525",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:46:54.650416Z",
     "iopub.status.busy": "2024-02-14T07:46:54.649374Z",
     "iopub.status.idle": "2024-02-14T07:46:54.665262Z",
     "shell.execute_reply": "2024-02-14T07:46:54.663925Z",
     "shell.execute_reply.started": "2024-02-14T07:46:54.650416Z"
    }
   },
   "outputs": [],
   "source": [
    "b = tf.convert_to_tensor(a, dtype=tf.int64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "64c478a7-0901-4f8b-b4d2-fae56346568f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:46:58.218795Z",
     "iopub.status.busy": "2024-02-14T07:46:58.217791Z",
     "iopub.status.idle": "2024-02-14T07:46:58.241782Z",
     "shell.execute_reply": "2024-02-14T07:46:58.239241Z",
     "shell.execute_reply.started": "2024-02-14T07:46:58.218795Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(2,), dtype=int64, numpy=array([1, 5], dtype=int64)>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8478d5a-2d53-46b2-97b0-cf54d6974104",
   "metadata": {},
   "source": [
    "#### 特殊张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "425daf9b-7fc0-43e6-895a-61ecbe9762ef",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:51:18.455662Z",
     "iopub.status.busy": "2024-02-14T07:51:18.454644Z",
     "iopub.status.idle": "2024-02-14T07:51:18.467846Z",
     "shell.execute_reply": "2024-02-14T07:51:18.466253Z",
     "shell.execute_reply.started": "2024-02-14T07:51:18.455662Z"
    }
   },
   "outputs": [],
   "source": [
    "a2 = tf.zeros([2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "567538a5-2a98-4f2b-85e7-2573675e8769",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:51:33.011305Z",
     "iopub.status.busy": "2024-02-14T07:51:33.010310Z",
     "iopub.status.idle": "2024-02-14T07:51:33.025678Z",
     "shell.execute_reply": "2024-02-14T07:51:33.023801Z",
     "shell.execute_reply.started": "2024-02-14T07:51:33.011305Z"
    }
   },
   "outputs": [],
   "source": [
    "b1 = tf.ones(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ea85f3a8-1534-4a45-89ef-387078db1a6f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:51:45.298149Z",
     "iopub.status.busy": "2024-02-14T07:51:45.298149Z",
     "iopub.status.idle": "2024-02-14T07:51:45.307161Z",
     "shell.execute_reply": "2024-02-14T07:51:45.305342Z",
     "shell.execute_reply.started": "2024-02-14T07:51:45.298149Z"
    }
   },
   "outputs": [],
   "source": [
    "c1 = tf.fill([2,2], 9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d758825b-80fc-43a5-89ef-2036847e2dd5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:52:21.486392Z",
     "iopub.status.busy": "2024-02-14T07:52:21.486392Z",
     "iopub.status.idle": "2024-02-14T07:52:21.494435Z",
     "shell.execute_reply": "2024-02-14T07:52:21.494062Z",
     "shell.execute_reply.started": "2024-02-14T07:52:21.486392Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(2, 3), dtype=float32, numpy=\n",
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.]], dtype=float32)>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "8b2ca10f-ad86-4d80-bedb-961901573b19",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:52:25.983819Z",
     "iopub.status.busy": "2024-02-14T07:52:25.982852Z",
     "iopub.status.idle": "2024-02-14T07:52:26.007641Z",
     "shell.execute_reply": "2024-02-14T07:52:26.006646Z",
     "shell.execute_reply.started": "2024-02-14T07:52:25.983819Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(4,), dtype=float32, numpy=array([1., 1., 1., 1.], dtype=float32)>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9b290fe1-e538-4229-b9bb-49443f3c838e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T07:52:29.859245Z",
     "iopub.status.busy": "2024-02-14T07:52:29.858241Z",
     "iopub.status.idle": "2024-02-14T07:52:29.876387Z",
     "shell.execute_reply": "2024-02-14T07:52:29.875385Z",
     "shell.execute_reply.started": "2024-02-14T07:52:29.859245Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(2, 2), dtype=int32, numpy=\n",
       "array([[9, 9],\n",
       "       [9, 9]])>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5444dcc4-6d6e-4aa7-ab06-128e59fef422",
   "metadata": {},
   "source": [
    "### 2. 常用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "4b04e9de-dfa7-429f-916d-3354106669d3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:08:59.847440Z",
     "iopub.status.busy": "2024-02-14T08:08:59.847440Z",
     "iopub.status.idle": "2024-02-14T08:08:59.868477Z",
     "shell.execute_reply": "2024-02-14T08:08:59.866578Z",
     "shell.execute_reply.started": "2024-02-14T08:08:59.847440Z"
    }
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "x1 = tf.constant([1.,2.,3.], dtype=tf.float64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "cc8eee1f-6481-4ca1-b831-de1601c5ea91",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:09:00.145384Z",
     "iopub.status.busy": "2024-02-14T08:09:00.143794Z",
     "iopub.status.idle": "2024-02-14T08:09:00.165538Z",
     "shell.execute_reply": "2024-02-14T08:09:00.163169Z",
     "shell.execute_reply.started": "2024-02-14T08:09:00.145384Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(3,), dtype=float64, numpy=array([1., 2., 3.])>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "64051694-f4d6-43a0-940a-40a42ea300ad",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:09:00.809407Z",
     "iopub.status.busy": "2024-02-14T08:09:00.808400Z",
     "iopub.status.idle": "2024-02-14T08:09:00.816050Z",
     "shell.execute_reply": "2024-02-14T08:09:00.814501Z",
     "shell.execute_reply.started": "2024-02-14T08:09:00.809407Z"
    }
   },
   "outputs": [],
   "source": [
    "x2 = tf.cast(x1,tf.int32) # 类型转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "9f2b7256-627e-443c-9e78-244fdc78bbc3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:09:01.239345Z",
     "iopub.status.busy": "2024-02-14T08:09:01.238345Z",
     "iopub.status.idle": "2024-02-14T08:09:01.249878Z",
     "shell.execute_reply": "2024-02-14T08:09:01.247886Z",
     "shell.execute_reply.started": "2024-02-14T08:09:01.239345Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3])>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a5a1ae61-d364-49a3-806e-138b877254d2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:09:01.530378Z",
     "iopub.status.busy": "2024-02-14T08:09:01.529366Z",
     "iopub.status.idle": "2024-02-14T08:09:01.539595Z",
     "shell.execute_reply": "2024-02-14T08:09:01.538338Z",
     "shell.execute_reply.started": "2024-02-14T08:09:01.530378Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(), dtype=int32, numpy=1>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.reduce_min(x2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "e0ad3ca9-a790-44c1-942e-26a224727ffb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:09:01.761284Z",
     "iopub.status.busy": "2024-02-14T08:09:01.760305Z",
     "iopub.status.idle": "2024-02-14T08:09:01.783900Z",
     "shell.execute_reply": "2024-02-14T08:09:01.781566Z",
     "shell.execute_reply.started": "2024-02-14T08:09:01.761284Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(), dtype=int32, numpy=3>"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.reduce_max(x2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "b3060cb6-2b33-46d1-9088-70dbaee65fa0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:09:02.017885Z",
     "iopub.status.busy": "2024-02-14T08:09:02.016905Z",
     "iopub.status.idle": "2024-02-14T08:09:02.028600Z",
     "shell.execute_reply": "2024-02-14T08:09:02.027594Z",
     "shell.execute_reply.started": "2024-02-14T08:09:02.017885Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(1, shape=(), dtype=int32) tf.Tensor(3, shape=(), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "print(tf.reduce_min(x2),tf.reduce_max(x2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae91da09-496d-40cc-9af3-76f0d3cc845c",
   "metadata": {},
   "source": [
    "#### axis 的理解"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "30bee100-b5db-4f90-85e8-b5707da19bb3",
   "metadata": {},
   "source": [
    "![axis.png](./img/axis.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9092f49c-6691-4a9f-a869-0e1c65268f26",
   "metadata": {},
   "source": [
    "#### 常用函数 平均数和总和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "57283df9-aa0e-4950-9830-24e9fa7e8097",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:10:15.656541Z",
     "iopub.status.busy": "2024-02-14T08:10:15.655537Z",
     "iopub.status.idle": "2024-02-14T08:10:15.676363Z",
     "shell.execute_reply": "2024-02-14T08:10:15.675256Z",
     "shell.execute_reply.started": "2024-02-14T08:10:15.656541Z"
    }
   },
   "outputs": [],
   "source": [
    "x1 = tf.constant([[1,2,3],\n",
    "                 [3,3,4]] \n",
    "                )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "de08cd5c-0447-4000-bec5-a54daf977eaf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:10:18.409900Z",
     "iopub.status.busy": "2024-02-14T08:10:18.409900Z",
     "iopub.status.idle": "2024-02-14T08:10:18.433291Z",
     "shell.execute_reply": "2024-02-14T08:10:18.431298Z",
     "shell.execute_reply.started": "2024-02-14T08:10:18.409900Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(2, 3), dtype=int32, numpy=\n",
       "array([[1, 2, 3],\n",
       "       [3, 3, 4]])>"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "c56b1970-bb9b-4636-b9da-80dffa40ee86",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:10:59.741562Z",
     "iopub.status.busy": "2024-02-14T08:10:59.740551Z",
     "iopub.status.idle": "2024-02-14T08:10:59.775250Z",
     "shell.execute_reply": "2024-02-14T08:10:59.773409Z",
     "shell.execute_reply.started": "2024-02-14T08:10:59.741562Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(), dtype=int32, numpy=2>"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.reduce_mean(x1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "206bb496-d2c9-4794-81e6-f90173e23426",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:12:01.162516Z",
     "iopub.status.busy": "2024-02-14T08:12:01.162516Z",
     "iopub.status.idle": "2024-02-14T08:12:01.179698Z",
     "shell.execute_reply": "2024-02-14T08:12:01.177841Z",
     "shell.execute_reply.started": "2024-02-14T08:12:01.162516Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(2,), dtype=int32, numpy=array([ 6, 10])>"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.reduce_sum(x1,axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c7be896-be6c-4aa7-8d78-dae4c66923b1",
   "metadata": {},
   "source": [
    "#### tf.Variable 标记为可训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "6f6e7c65-6461-446a-bcd5-a3c2798fcdd2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:15:01.435656Z",
     "iopub.status.busy": "2024-02-14T08:15:01.434653Z",
     "iopub.status.idle": "2024-02-14T08:15:01.471260Z",
     "shell.execute_reply": "2024-02-14T08:15:01.468181Z",
     "shell.execute_reply.started": "2024-02-14T08:15:01.435656Z"
    }
   },
   "outputs": [],
   "source": [
    "w = tf.Variable(tf.random.normal([2,2],mean=0,stddev=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "fbc31560-f73d-41e8-bc3b-30d6e9673a6e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:15:04.157083Z",
     "iopub.status.busy": "2024-02-14T08:15:04.155953Z",
     "iopub.status.idle": "2024-02-14T08:15:04.176783Z",
     "shell.execute_reply": "2024-02-14T08:15:04.175858Z",
     "shell.execute_reply.started": "2024-02-14T08:15:04.157083Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=\n",
       "array([[-0.6943884 ,  0.1465757 ],\n",
       "       [ 0.06692058, -0.14852549]], dtype=float32)>"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46919cdc-6ae9-41b2-a0ee-4db27fee98cc",
   "metadata": {},
   "source": [
    "#### 其他数学运算\n",
    ">  ![tf-base-01.png](./img/tf-base-01.png)\n",
    ">\n",
    ">  ![tf-base-02.png](./img/tf-base-02.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "d79c263b-cc4d-4693-8ba7-f6e17f2e4f7e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:20:57.814088Z",
     "iopub.status.busy": "2024-02-14T08:20:57.813930Z",
     "iopub.status.idle": "2024-02-14T08:20:57.838814Z",
     "shell.execute_reply": "2024-02-14T08:20:57.835713Z",
     "shell.execute_reply.started": "2024-02-14T08:20:57.814088Z"
    }
   },
   "outputs": [],
   "source": [
    "a = tf.ones([1,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "5e58084a-b55a-4387-aa36-97ba7ccc0b09",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:21:18.036310Z",
     "iopub.status.busy": "2024-02-14T08:21:18.036310Z",
     "iopub.status.idle": "2024-02-14T08:21:18.057023Z",
     "shell.execute_reply": "2024-02-14T08:21:18.054264Z",
     "shell.execute_reply.started": "2024-02-14T08:21:18.036310Z"
    }
   },
   "outputs": [],
   "source": [
    "b = tf.fill([1,3],3.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "e57ec98c-265f-42ac-ae7a-c6cd0327bd73",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:21:19.816795Z",
     "iopub.status.busy": "2024-02-14T08:21:19.815130Z",
     "iopub.status.idle": "2024-02-14T08:21:19.830586Z",
     "shell.execute_reply": "2024-02-14T08:21:19.829333Z",
     "shell.execute_reply.started": "2024-02-14T08:21:19.816795Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[1., 1., 1.]], dtype=float32)>"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "907c7aa3-88f0-4b0f-8a33-733b6de9c2e8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:21:22.373987Z",
     "iopub.status.busy": "2024-02-14T08:21:22.372479Z",
     "iopub.status.idle": "2024-02-14T08:21:22.391174Z",
     "shell.execute_reply": "2024-02-14T08:21:22.390186Z",
     "shell.execute_reply.started": "2024-02-14T08:21:22.373987Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[3., 3., 3.]], dtype=float32)>"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "fd10f009-a39f-4441-bf04-24bebe85b61b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:21:37.957300Z",
     "iopub.status.busy": "2024-02-14T08:21:37.957300Z",
     "iopub.status.idle": "2024-02-14T08:21:38.015568Z",
     "shell.execute_reply": "2024-02-14T08:21:38.012038Z",
     "shell.execute_reply.started": "2024-02-14T08:21:37.957300Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[4., 4., 4.]], dtype=float32)>"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.add(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "b854fe6e-25b4-4900-aa2e-1c6183e27627",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:21:53.045995Z",
     "iopub.status.busy": "2024-02-14T08:21:53.045995Z",
     "iopub.status.idle": "2024-02-14T08:21:53.099624Z",
     "shell.execute_reply": "2024-02-14T08:21:53.097366Z",
     "shell.execute_reply.started": "2024-02-14T08:21:53.045995Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[-2., -2., -2.]], dtype=float32)>"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.subtract(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "5abbe7ac-b2ed-4697-b08d-0cf7fedaad3a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:22:56.428471Z",
     "iopub.status.busy": "2024-02-14T08:22:56.428471Z",
     "iopub.status.idle": "2024-02-14T08:22:56.465140Z",
     "shell.execute_reply": "2024-02-14T08:22:56.462698Z",
     "shell.execute_reply.started": "2024-02-14T08:22:56.428471Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[3., 3., 3.]], dtype=float32)>"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.multiply(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "efebbd2a-045a-4d79-9303-a7383de59bb6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:23:21.765705Z",
     "iopub.status.busy": "2024-02-14T08:23:21.764705Z",
     "iopub.status.idle": "2024-02-14T08:23:21.812210Z",
     "shell.execute_reply": "2024-02-14T08:23:21.809369Z",
     "shell.execute_reply.started": "2024-02-14T08:23:21.765705Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[3., 3., 3.]], dtype=float32)>"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.divide(b,a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb058a6c-e99e-48ea-b6c2-eb9a151b82f5",
   "metadata": {},
   "source": [
    "#### 平方次方开方计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "22096c21-0c5c-46cc-a087-d070665aa612",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:31:09.994538Z",
     "iopub.status.busy": "2024-02-14T08:31:09.994538Z",
     "iopub.status.idle": "2024-02-14T08:31:10.008335Z",
     "shell.execute_reply": "2024-02-14T08:31:10.006662Z",
     "shell.execute_reply.started": "2024-02-14T08:31:09.994538Z"
    }
   },
   "outputs": [],
   "source": [
    "a3 = tf.fill([1,2],3.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "aa5c30a2-a670-4e2d-ab6b-dc84fcd99a45",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:31:10.781916Z",
     "iopub.status.busy": "2024-02-14T08:31:10.780955Z",
     "iopub.status.idle": "2024-02-14T08:31:10.801015Z",
     "shell.execute_reply": "2024-02-14T08:31:10.799165Z",
     "shell.execute_reply.started": "2024-02-14T08:31:10.781916Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 2), dtype=float32, numpy=array([[3., 3.]], dtype=float32)>"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "318cf079-1937-455a-aba1-449bea1bfff3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:31:11.830051Z",
     "iopub.status.busy": "2024-02-14T08:31:11.830051Z",
     "iopub.status.idle": "2024-02-14T08:31:11.864230Z",
     "shell.execute_reply": "2024-02-14T08:31:11.863220Z",
     "shell.execute_reply.started": "2024-02-14T08:31:11.830051Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 2), dtype=float32, numpy=array([[9., 9.]], dtype=float32)>"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.square(a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "e28965ec-3900-4dd5-abef-c1eefdf105c6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:31:12.621141Z",
     "iopub.status.busy": "2024-02-14T08:31:12.620139Z",
     "iopub.status.idle": "2024-02-14T08:31:12.640154Z",
     "shell.execute_reply": "2024-02-14T08:31:12.639447Z",
     "shell.execute_reply.started": "2024-02-14T08:31:12.621141Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 2), dtype=float32, numpy=array([[27., 27.]], dtype=float32)>"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.pow(a3, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "fa25a979-3e24-45f0-8fe3-42e9e512d18c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:31:34.771278Z",
     "iopub.status.busy": "2024-02-14T08:31:34.769768Z",
     "iopub.status.idle": "2024-02-14T08:31:34.783660Z",
     "shell.execute_reply": "2024-02-14T08:31:34.781947Z",
     "shell.execute_reply.started": "2024-02-14T08:31:34.771278Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 2), dtype=float32, numpy=array([[1.7320508, 1.7320508]], dtype=float32)>"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.sqrt(a3) # 要求数据类型为float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "fe63c15a-b36e-4257-abfd-33d214234dc9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:37:43.092677Z",
     "iopub.status.busy": "2024-02-14T08:37:43.091693Z",
     "iopub.status.idle": "2024-02-14T08:37:43.106633Z",
     "shell.execute_reply": "2024-02-14T08:37:43.104850Z",
     "shell.execute_reply.started": "2024-02-14T08:37:43.092677Z"
    }
   },
   "outputs": [],
   "source": [
    "a4 = tf.ones([3,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "e4a73efd-83b1-4d54-b223-43012491b4da",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:37:45.144839Z",
     "iopub.status.busy": "2024-02-14T08:37:45.143568Z",
     "iopub.status.idle": "2024-02-14T08:37:45.155725Z",
     "shell.execute_reply": "2024-02-14T08:37:45.154197Z",
     "shell.execute_reply.started": "2024-02-14T08:37:45.144839Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(3, 2), dtype=float32, numpy=\n",
       "array([[1., 1.],\n",
       "       [1., 1.],\n",
       "       [1., 1.]], dtype=float32)>"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "91a8b85e-f606-44d2-adf9-b1bf30753ea9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:38:05.907998Z",
     "iopub.status.busy": "2024-02-14T08:38:05.907998Z",
     "iopub.status.idle": "2024-02-14T08:38:05.928415Z",
     "shell.execute_reply": "2024-02-14T08:38:05.927625Z",
     "shell.execute_reply.started": "2024-02-14T08:38:05.907998Z"
    }
   },
   "outputs": [],
   "source": [
    "b3 = tf.fill([2,3],3.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "ee6c6e27-c7fd-4693-a974-e0fc21eb416d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:38:09.409417Z",
     "iopub.status.busy": "2024-02-14T08:38:09.408419Z",
     "iopub.status.idle": "2024-02-14T08:38:09.428920Z",
     "shell.execute_reply": "2024-02-14T08:38:09.427992Z",
     "shell.execute_reply.started": "2024-02-14T08:38:09.409417Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(2, 3), dtype=float32, numpy=\n",
       "array([[3., 3., 3.],\n",
       "       [3., 3., 3.]], dtype=float32)>"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "a36b43c6-8e01-40b4-9bc5-eee3f26dde28",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:40:19.877042Z",
     "iopub.status.busy": "2024-02-14T08:40:19.877042Z",
     "iopub.status.idle": "2024-02-14T08:40:19.891372Z",
     "shell.execute_reply": "2024-02-14T08:40:19.890287Z",
     "shell.execute_reply.started": "2024-02-14T08:40:19.877042Z"
    }
   },
   "outputs": [],
   "source": [
    "# mat = tf.matmul(a4,b3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "6c34a6c8-d8ff-424d-8eca-61989d33d4fd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:40:33.705144Z",
     "iopub.status.busy": "2024-02-14T08:40:33.705144Z",
     "iopub.status.idle": "2024-02-14T08:40:33.725276Z",
     "shell.execute_reply": "2024-02-14T08:40:33.723580Z",
     "shell.execute_reply.started": "2024-02-14T08:40:33.705144Z"
    }
   },
   "outputs": [],
   "source": [
    "with tf.device(\"/gpu:0\"):\n",
    "   mat = tf.matmul(a4,b3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "614fd72b-d157-4853-9608-2d57ad5cbc8d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:40:35.923838Z",
     "iopub.status.busy": "2024-02-14T08:40:35.922880Z",
     "iopub.status.idle": "2024-02-14T08:40:35.946061Z",
     "shell.execute_reply": "2024-02-14T08:40:35.945060Z",
     "shell.execute_reply.started": "2024-02-14T08:40:35.923838Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(3, 3), dtype=float32, numpy=\n",
       "array([[6., 6., 6.],\n",
       "       [6., 6., 6.],\n",
       "       [6., 6., 6.]], dtype=float32)>"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d510f96-5acd-4b60-9767-2e5900bee41f",
   "metadata": {},
   "source": [
    "#### 切分数据\n",
    "> tf.data.Dataset.from_ternsor_slices\n",
    ">\n",
    "> ![tf-base-03.png](./img/tf-base-03.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "e1c9ac5b-beb2-4354-be9e-782cb5091205",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:44:04.016223Z",
     "iopub.status.busy": "2024-02-14T08:44:04.014220Z",
     "iopub.status.idle": "2024-02-14T08:44:04.025263Z",
     "shell.execute_reply": "2024-02-14T08:44:04.023743Z",
     "shell.execute_reply.started": "2024-02-14T08:44:04.016223Z"
    }
   },
   "outputs": [],
   "source": [
    "featrues = tf.constant([12,23,10,17])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "c6856dc6-b056-4802-bf13-ebfe22c52bc9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:44:11.234162Z",
     "iopub.status.busy": "2024-02-14T08:44:11.234162Z",
     "iopub.status.idle": "2024-02-14T08:44:11.255531Z",
     "shell.execute_reply": "2024-02-14T08:44:11.253517Z",
     "shell.execute_reply.started": "2024-02-14T08:44:11.234162Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(4,), dtype=int32, numpy=array([12, 23, 10, 17])>"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "featrues"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "39056cf4-fa3f-4c3f-b6bb-5fa172985664",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:44:33.777026Z",
     "iopub.status.busy": "2024-02-14T08:44:33.777026Z",
     "iopub.status.idle": "2024-02-14T08:44:33.785189Z",
     "shell.execute_reply": "2024-02-14T08:44:33.783195Z",
     "shell.execute_reply.started": "2024-02-14T08:44:33.777026Z"
    }
   },
   "outputs": [],
   "source": [
    "labels = tf.constant([0,1,1,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "663cdf84-8745-4103-ab1e-ab88a97a8a3e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:45:16.932798Z",
     "iopub.status.busy": "2024-02-14T08:45:16.931779Z",
     "iopub.status.idle": "2024-02-14T08:45:16.963316Z",
     "shell.execute_reply": "2024-02-14T08:45:16.961732Z",
     "shell.execute_reply.started": "2024-02-14T08:45:16.932798Z"
    }
   },
   "outputs": [],
   "source": [
    "dataset = tf.data.Dataset.from_tensor_slices((featrues,labels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "f215a08a-9bcb-43d3-94d3-b56cbac7d6ee",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:45:20.428822Z",
     "iopub.status.busy": "2024-02-14T08:45:20.427830Z",
     "iopub.status.idle": "2024-02-14T08:45:20.437748Z",
     "shell.execute_reply": "2024-02-14T08:45:20.436571Z",
     "shell.execute_reply.started": "2024-02-14T08:45:20.428822Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<TensorSliceDataset element_spec=(TensorSpec(shape=(), dtype=tf.int32, name=None), TensorSpec(shape=(), dtype=tf.int32, name=None))>"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "e21c725c-9f15-4601-a482-0fa58eef5a30",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:46:36.820045Z",
     "iopub.status.busy": "2024-02-14T08:46:36.819033Z",
     "iopub.status.idle": "2024-02-14T08:46:36.841543Z",
     "shell.execute_reply": "2024-02-14T08:46:36.839381Z",
     "shell.execute_reply.started": "2024-02-14T08:46:36.820045Z"
    }
   },
   "outputs": [],
   "source": [
    "def output_dataset(dataset):\n",
    "    for ele in dataset:\n",
    "        print(ele)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "ae7c71f8-40c9-4540-a72c-819490348f07",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:46:45.302012Z",
     "iopub.status.busy": "2024-02-14T08:46:45.301009Z",
     "iopub.status.idle": "2024-02-14T08:46:45.395585Z",
     "shell.execute_reply": "2024-02-14T08:46:45.393569Z",
     "shell.execute_reply.started": "2024-02-14T08:46:45.302012Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(<tf.Tensor: shape=(), dtype=int32, numpy=12>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)\n",
      "(<tf.Tensor: shape=(), dtype=int32, numpy=23>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)\n",
      "(<tf.Tensor: shape=(), dtype=int32, numpy=10>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)\n",
      "(<tf.Tensor: shape=(), dtype=int32, numpy=17>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)\n"
     ]
    }
   ],
   "source": [
    "output_dataset(dataset)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f90a54f-754e-4a6f-b4ba-7136b1292760",
   "metadata": {},
   "source": [
    "#### 求导运算\n",
    ">\n",
    ">![tf-base-04.png](./img/tf-base-04.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "39c8871f-bcd9-4b08-b311-44f5c52347d5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:51:19.302082Z",
     "iopub.status.busy": "2024-02-14T08:51:19.301080Z",
     "iopub.status.idle": "2024-02-14T08:51:19.323250Z",
     "shell.execute_reply": "2024-02-14T08:51:19.322122Z",
     "shell.execute_reply.started": "2024-02-14T08:51:19.302082Z"
    }
   },
   "outputs": [],
   "source": [
    "with tf.GradientTape() as tape:\n",
    "     w = tf.Variable(tf.constant(3.0))\n",
    "     loss = tf.pow(w,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "83abc497-6a26-457e-8af3-583218c4e5f9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:51:21.520017Z",
     "iopub.status.busy": "2024-02-14T08:51:21.520017Z",
     "iopub.status.idle": "2024-02-14T08:51:21.546379Z",
     "shell.execute_reply": "2024-02-14T08:51:21.542698Z",
     "shell.execute_reply.started": "2024-02-14T08:51:21.520017Z"
    }
   },
   "outputs": [],
   "source": [
    "grad = tape.gradient(loss,w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "df840993-0a2d-40e1-967b-386d2f221e5b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:51:22.349710Z",
     "iopub.status.busy": "2024-02-14T08:51:22.348711Z",
     "iopub.status.idle": "2024-02-14T08:51:22.373384Z",
     "shell.execute_reply": "2024-02-14T08:51:22.371229Z",
     "shell.execute_reply.started": "2024-02-14T08:51:22.349710Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(), dtype=float32, numpy=6.0>"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grad"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9c59100-5d60-4adb-a46b-20f4ecc924c8",
   "metadata": {},
   "source": [
    "#### 组合索引\n",
    ">\n",
    ">![tf-base-05.png](./img/tf-base-05.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "7c601211-cbce-4b60-b1b3-856772a55cf4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:53:16.941229Z",
     "iopub.status.busy": "2024-02-14T08:53:16.940264Z",
     "iopub.status.idle": "2024-02-14T08:53:16.947521Z",
     "shell.execute_reply": "2024-02-14T08:53:16.946227Z",
     "shell.execute_reply.started": "2024-02-14T08:53:16.941229Z"
    }
   },
   "outputs": [],
   "source": [
    "seq = ['one', 'two', 'three']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "1323b310-6c00-4f6f-a613-3c37d76521cb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:53:44.479913Z",
     "iopub.status.busy": "2024-02-14T08:53:44.479913Z",
     "iopub.status.idle": "2024-02-14T08:53:44.489776Z",
     "shell.execute_reply": "2024-02-14T08:53:44.488761Z",
     "shell.execute_reply.started": "2024-02-14T08:53:44.479913Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 one\n",
      "1 two\n",
      "2 three\n"
     ]
    }
   ],
   "source": [
    "for i,ele in enumerate(seq):\n",
    "    print(i,ele)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53df5f10-83d0-45bc-b791-c6a44f41f343",
   "metadata": {},
   "source": [
    "#### 独热编码\n",
    ">\n",
    ">![tf-base-06.png](./img/tf-base-06.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "f621abcd-4ba3-4903-a8eb-17e67f6c4904",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:56:16.315001Z",
     "iopub.status.busy": "2024-02-14T08:56:16.313654Z",
     "iopub.status.idle": "2024-02-14T08:56:16.325281Z",
     "shell.execute_reply": "2024-02-14T08:56:16.323734Z",
     "shell.execute_reply.started": "2024-02-14T08:56:16.315001Z"
    }
   },
   "outputs": [],
   "source": [
    "classes = 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "7cf6c4e0-f72e-4c76-88f5-169a9b1f8325",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:56:33.374764Z",
     "iopub.status.busy": "2024-02-14T08:56:33.373759Z",
     "iopub.status.idle": "2024-02-14T08:56:33.382558Z",
     "shell.execute_reply": "2024-02-14T08:56:33.381162Z",
     "shell.execute_reply.started": "2024-02-14T08:56:33.374764Z"
    }
   },
   "outputs": [],
   "source": [
    "labels = tf.constant([1,0,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "fa85c3b3-94e1-40b7-b87d-7ab0a6045fc8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:56:37.884332Z",
     "iopub.status.busy": "2024-02-14T08:56:37.884332Z",
     "iopub.status.idle": "2024-02-14T08:56:37.898047Z",
     "shell.execute_reply": "2024-02-14T08:56:37.896267Z",
     "shell.execute_reply.started": "2024-02-14T08:56:37.884332Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 0, 2])>"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "6775a8b0-7ab1-4b98-8f04-29481499785f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:57:03.499280Z",
     "iopub.status.busy": "2024-02-14T08:57:03.497579Z",
     "iopub.status.idle": "2024-02-14T08:57:03.523058Z",
     "shell.execute_reply": "2024-02-14T08:57:03.519139Z",
     "shell.execute_reply.started": "2024-02-14T08:57:03.499280Z"
    }
   },
   "outputs": [],
   "source": [
    "output = tf.one_hot(labels,depth= classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "1a87df44-0c1b-4983-8d31-0dce0ecea863",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T08:57:12.713040Z",
     "iopub.status.busy": "2024-02-14T08:57:12.712041Z",
     "iopub.status.idle": "2024-02-14T08:57:12.723733Z",
     "shell.execute_reply": "2024-02-14T08:57:12.722725Z",
     "shell.execute_reply.started": "2024-02-14T08:57:12.713040Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(3, 3), dtype=float32, numpy=\n",
       "array([[0., 1., 0.],\n",
       "       [1., 0., 0.],\n",
       "       [0., 0., 1.]], dtype=float32)>"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20a21894-44b0-4f44-92bd-ea1cda5662bb",
   "metadata": {},
   "source": [
    "#### tf.nn.softmax 前向传播\n",
    ">\n",
    ">![tf-base-07.png](./img/tf-base-07.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "6adc24c5-41c6-4359-9186-d64a16149c30",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:01:52.865071Z",
     "iopub.status.busy": "2024-02-14T09:01:52.865071Z",
     "iopub.status.idle": "2024-02-14T09:01:52.879298Z",
     "shell.execute_reply": "2024-02-14T09:01:52.877853Z",
     "shell.execute_reply.started": "2024-02-14T09:01:52.865071Z"
    }
   },
   "outputs": [],
   "source": [
    "y = tf.constant([1.01,2.01,-0.66])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "f837451f-922e-4465-a112-4416b3defce0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:02:17.926459Z",
     "iopub.status.busy": "2024-02-14T09:02:17.925002Z",
     "iopub.status.idle": "2024-02-14T09:02:17.951616Z",
     "shell.execute_reply": "2024-02-14T09:02:17.949369Z",
     "shell.execute_reply.started": "2024-02-14T09:02:17.926459Z"
    }
   },
   "outputs": [],
   "source": [
    "y_pro = tf.nn.softmax(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "41cc1377-5ada-4322-8528-fad71109ffb8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:02:22.431647Z",
     "iopub.status.busy": "2024-02-14T09:02:22.430631Z",
     "iopub.status.idle": "2024-02-14T09:02:22.444917Z",
     "shell.execute_reply": "2024-02-14T09:02:22.443862Z",
     "shell.execute_reply.started": "2024-02-14T09:02:22.431647Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0.25598174, 0.6958304 , 0.0481878 ], dtype=float32)>"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pro"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99ca15af-d1ae-4c9e-89bd-6301ae59b4d2",
   "metadata": {},
   "source": [
    "#### assign_sub\n",
    ">\n",
    ">![tf-base-08.png](./img/tf-base-08.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "ea201b52-ca89-4bb1-9087-bd59fd591297",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:04:58.268032Z",
     "iopub.status.busy": "2024-02-14T09:04:58.266924Z",
     "iopub.status.idle": "2024-02-14T09:04:58.291049Z",
     "shell.execute_reply": "2024-02-14T09:04:58.289226Z",
     "shell.execute_reply.started": "2024-02-14T09:04:58.268032Z"
    }
   },
   "outputs": [],
   "source": [
    "w = tf.Variable(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "55db8215-7e8f-4f45-8165-c8373e6bf5a9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:05:01.676052Z",
     "iopub.status.busy": "2024-02-14T09:05:01.675024Z",
     "iopub.status.idle": "2024-02-14T09:05:01.698082Z",
     "shell.execute_reply": "2024-02-14T09:05:01.695675Z",
     "shell.execute_reply.started": "2024-02-14T09:05:01.676052Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=4>"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "bd530e8f-5c18-47ad-bcaf-665bc25b18b7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:05:23.686860Z",
     "iopub.status.busy": "2024-02-14T09:05:23.686860Z",
     "iopub.status.idle": "2024-02-14T09:05:23.707887Z",
     "shell.execute_reply": "2024-02-14T09:05:23.705887Z",
     "shell.execute_reply.started": "2024-02-14T09:05:23.686860Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Variable 'UnreadVariable' shape=() dtype=int32, numpy=3>"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.assign_sub(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "id": "d4502c91-fd9d-42ac-847d-f02c65d461c9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:05:25.748886Z",
     "iopub.status.busy": "2024-02-14T09:05:25.748886Z",
     "iopub.status.idle": "2024-02-14T09:05:25.768693Z",
     "shell.execute_reply": "2024-02-14T09:05:25.767688Z",
     "shell.execute_reply.started": "2024-02-14T09:05:25.748886Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d46549af-0613-41f4-888c-eaa8d2d9bfea",
   "metadata": {},
   "source": [
    "#### tf.argmax\n",
    "> 返回某一个方向上的做大值索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "ae0f83a8-13e5-410b-8d94-e4a8035b69e6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:06:58.269138Z",
     "iopub.status.busy": "2024-02-14T09:06:58.269138Z",
     "iopub.status.idle": "2024-02-14T09:06:58.279264Z",
     "shell.execute_reply": "2024-02-14T09:06:58.277687Z",
     "shell.execute_reply.started": "2024-02-14T09:06:58.269138Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "id": "2a87a915-ca1d-40f2-9cfb-ab5333e36359",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:07:48.294989Z",
     "iopub.status.busy": "2024-02-14T09:07:48.294989Z",
     "iopub.status.idle": "2024-02-14T09:07:48.316683Z",
     "shell.execute_reply": "2024-02-14T09:07:48.315052Z",
     "shell.execute_reply.started": "2024-02-14T09:07:48.294989Z"
    }
   },
   "outputs": [],
   "source": [
    "test = np.array([[1,2,3],\n",
    "                 [2,3,4],\n",
    "                 [5,4,3],\n",
    "                 [8,7,2]\n",
    "                ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "91d368a3-a131-4867-a9b4-fd40cd363579",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:07:50.627809Z",
     "iopub.status.busy": "2024-02-14T09:07:50.627809Z",
     "iopub.status.idle": "2024-02-14T09:07:50.648568Z",
     "shell.execute_reply": "2024-02-14T09:07:50.646178Z",
     "shell.execute_reply.started": "2024-02-14T09:07:50.627809Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 3, 4],\n",
       "       [5, 4, 3],\n",
       "       [8, 7, 2]])"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "f075e302-fb20-416c-b559-2cb5e2e83da9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:08:12.307903Z",
     "iopub.status.busy": "2024-02-14T09:08:12.306934Z",
     "iopub.status.idle": "2024-02-14T09:08:12.344261Z",
     "shell.execute_reply": "2024-02-14T09:08:12.342260Z",
     "shell.execute_reply.started": "2024-02-14T09:08:12.306934Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(3,), dtype=int64, numpy=array([3, 3, 1], dtype=int64)>"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.argmax(test,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "id": "12945be6-4eff-49a0-b413-f667ebd4786f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:09:02.438846Z",
     "iopub.status.busy": "2024-02-14T09:09:02.437728Z",
     "iopub.status.idle": "2024-02-14T09:09:02.461218Z",
     "shell.execute_reply": "2024-02-14T09:09:02.459219Z",
     "shell.execute_reply.started": "2024-02-14T09:09:02.438846Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(4,), dtype=int64, numpy=array([2, 2, 0, 0], dtype=int64)>"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.argmax(test,axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2e1fd2f-9a01-4f39-87f3-a9d8fb456d3c",
   "metadata": {},
   "source": [
    "#### tf.where"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "id": "931b375c-4006-4043-bff6-993e1d111d1e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:54:12.623655Z",
     "iopub.status.busy": "2024-02-14T09:54:12.623655Z",
     "iopub.status.idle": "2024-02-14T09:54:12.635223Z",
     "shell.execute_reply": "2024-02-14T09:54:12.634002Z",
     "shell.execute_reply.started": "2024-02-14T09:54:12.623655Z"
    }
   },
   "outputs": [],
   "source": [
    "a = tf.constant([1,2,3,1,1])\n",
    "b = tf.constant([0,1,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "9867f143-909c-49a0-b149-77696b25d249",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:54:48.044928Z",
     "iopub.status.busy": "2024-02-14T09:54:48.044928Z",
     "iopub.status.idle": "2024-02-14T09:54:50.818409Z",
     "shell.execute_reply": "2024-02-14T09:54:50.815422Z",
     "shell.execute_reply.started": "2024-02-14T09:54:48.044928Z"
    }
   },
   "outputs": [],
   "source": [
    "c = tf.where(tf.greater(a,b),a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "d822ea7c-a480-4ae2-8ef2-9b26efdda725",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:54:55.607085Z",
     "iopub.status.busy": "2024-02-14T09:54:55.607085Z",
     "iopub.status.idle": "2024-02-14T09:54:55.623329Z",
     "shell.execute_reply": "2024-02-14T09:54:55.622334Z",
     "shell.execute_reply.started": "2024-02-14T09:54:55.607085Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5,), dtype=int32, numpy=array([1, 2, 3, 4, 5])>"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b40332c0-1911-4e91-a2d4-352b02d2767d",
   "metadata": {},
   "source": [
    "#### numpy 随机数生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "e76beccc-4cca-4226-9f81-bfef8645d5df",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:56:34.017927Z",
     "iopub.status.busy": "2024-02-14T09:56:34.016918Z",
     "iopub.status.idle": "2024-02-14T09:56:34.035103Z",
     "shell.execute_reply": "2024-02-14T09:56:34.033099Z",
     "shell.execute_reply.started": "2024-02-14T09:56:34.017927Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "f4e2d8b5-16de-45f1-bd82-0d6a21028231",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:57:41.503827Z",
     "iopub.status.busy": "2024-02-14T09:57:41.502892Z",
     "iopub.status.idle": "2024-02-14T09:57:41.516832Z",
     "shell.execute_reply": "2024-02-14T09:57:41.513846Z",
     "shell.execute_reply.started": "2024-02-14T09:57:41.503827Z"
    }
   },
   "outputs": [],
   "source": [
    "rdm = np.random.RandomState(seed=1)\n",
    "a = rdm.rand()\n",
    "b = rdm.rand(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "b1ca8299-d892-43e9-98e4-fda52834c696",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:57:44.082746Z",
     "iopub.status.busy": "2024-02-14T09:57:44.081750Z",
     "iopub.status.idle": "2024-02-14T09:57:44.097807Z",
     "shell.execute_reply": "2024-02-14T09:57:44.096689Z",
     "shell.execute_reply.started": "2024-02-14T09:57:44.082746Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.417022004702574"
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "5044f51d-b7f1-49eb-89fa-cd6af16c2093",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:57:46.048383Z",
     "iopub.status.busy": "2024-02-14T09:57:46.048383Z",
     "iopub.status.idle": "2024-02-14T09:57:46.071811Z",
     "shell.execute_reply": "2024-02-14T09:57:46.069234Z",
     "shell.execute_reply.started": "2024-02-14T09:57:46.048383Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7.20324493e-01, 1.14374817e-04, 3.02332573e-01],\n",
       "       [1.46755891e-01, 9.23385948e-02, 1.86260211e-01]])"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8efe6066-ca16-47fc-a7ce-ae0252b6f310",
   "metadata": {},
   "source": [
    "#### numpy.vstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "id": "8337ae34-a442-476f-a802-ab8533113a4e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:58:53.606632Z",
     "iopub.status.busy": "2024-02-14T09:58:53.605636Z",
     "iopub.status.idle": "2024-02-14T09:58:53.620353Z",
     "shell.execute_reply": "2024-02-14T09:58:53.617359Z",
     "shell.execute_reply.started": "2024-02-14T09:58:53.606632Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "70dc0a8b-671e-44a0-b8a1-6cb3e1c88281",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:59:05.801332Z",
     "iopub.status.busy": "2024-02-14T09:59:05.801332Z",
     "iopub.status.idle": "2024-02-14T09:59:05.809543Z",
     "shell.execute_reply": "2024-02-14T09:59:05.808232Z",
     "shell.execute_reply.started": "2024-02-14T09:59:05.801332Z"
    }
   },
   "outputs": [],
   "source": [
    "b = np.array([4,5,6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "67f84c55-e348-49c3-b23f-fbe56028c76a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:59:20.052538Z",
     "iopub.status.busy": "2024-02-14T09:59:20.051683Z",
     "iopub.status.idle": "2024-02-14T09:59:20.069211Z",
     "shell.execute_reply": "2024-02-14T09:59:20.067018Z",
     "shell.execute_reply.started": "2024-02-14T09:59:20.052538Z"
    }
   },
   "outputs": [],
   "source": [
    "c = np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "a0248b79-0a6a-4e04-a73d-63413bb9b6c2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:59:21.796948Z",
     "iopub.status.busy": "2024-02-14T09:59:21.795814Z",
     "iopub.status.idle": "2024-02-14T09:59:21.813432Z",
     "shell.execute_reply": "2024-02-14T09:59:21.811902Z",
     "shell.execute_reply.started": "2024-02-14T09:59:21.796948Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74c81206-1afc-4a24-95ad-30e86e6d8f90",
   "metadata": {},
   "source": [
    "#### np.mgrid ravel c_[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "054c3462-b410-40eb-8f4d-01d4ce8886a9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T10:02:35.845083Z",
     "iopub.status.busy": "2024-02-14T10:02:35.844212Z",
     "iopub.status.idle": "2024-02-14T10:02:35.862341Z",
     "shell.execute_reply": "2024-02-14T10:02:35.861341Z",
     "shell.execute_reply.started": "2024-02-14T10:02:35.845083Z"
    }
   },
   "outputs": [],
   "source": [
    "x,y = np.mgrid[1:3:1,2:4:0.5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "id": "edc58128-395a-425b-8366-661df29cffd8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T10:02:36.616617Z",
     "iopub.status.busy": "2024-02-14T10:02:36.615678Z",
     "iopub.status.idle": "2024-02-14T10:02:36.626344Z",
     "shell.execute_reply": "2024-02-14T10:02:36.624888Z",
     "shell.execute_reply.started": "2024-02-14T10:02:36.616617Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [2., 2., 2., 2.]])"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "id": "a009e6fa-de69-4ba0-89cb-924ff779ecb3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T10:02:37.546439Z",
     "iopub.status.busy": "2024-02-14T10:02:37.543999Z",
     "iopub.status.idle": "2024-02-14T10:02:37.572202Z",
     "shell.execute_reply": "2024-02-14T10:02:37.568227Z",
     "shell.execute_reply.started": "2024-02-14T10:02:37.546439Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2. , 2.5, 3. , 3.5],\n",
       "       [2. , 2.5, 3. , 3.5]])"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "86e613d6-c3a4-4680-b702-8cbe2664f4ab",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T10:03:19.858260Z",
     "iopub.status.busy": "2024-02-14T10:03:19.857261Z",
     "iopub.status.idle": "2024-02-14T10:03:19.872836Z",
     "shell.execute_reply": "2024-02-14T10:03:19.870277Z",
     "shell.execute_reply.started": "2024-02-14T10:03:19.858260Z"
    }
   },
   "outputs": [],
   "source": [
    "grid = np.c_[x.ravel(),y.ravel()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "fba42662-67db-4151-8cc2-1c9414192ac6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T10:03:23.478033Z",
     "iopub.status.busy": "2024-02-14T10:03:23.477014Z",
     "iopub.status.idle": "2024-02-14T10:03:23.503170Z",
     "shell.execute_reply": "2024-02-14T10:03:23.501254Z",
     "shell.execute_reply.started": "2024-02-14T10:03:23.478033Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1. , 2. ],\n",
       "       [1. , 2.5],\n",
       "       [1. , 3. ],\n",
       "       [1. , 3.5],\n",
       "       [2. , 2. ],\n",
       "       [2. , 2.5],\n",
       "       [2. , 3. ],\n",
       "       [2. , 3.5]])"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4726fd36-c92c-4ae7-becf-5072d9ebe5b8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf2.10",
   "language": "python",
   "name": "tf2.10"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  },
  "scenes_data": {
   "active_scene": "Default Scene",
   "init_scene": "",
   "scenes": [
    "Default Scene"
   ]
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
